import React from "react";
import { Link, Routes, Route, useParams, useLocation } from "react-router-dom";

const Home = () => {
  return <div>home组件</div>;
};

const About = () => {
  // query传参和接收
  const { search } = useLocation();
  // console.log(location);
  console.log(new URLSearchParams(search).get("a"));

  return <div>about页面</div>;
};

const Detail = () => {
  const { id } = useParams();
  return <div>detail页面 - {id}</div>;
};

const App = () => {
  return (
    <>
      <h3>router-v6-动态可选参数</h3>
      <ul>
        <li>
          {/* 完整的通过对象的方式进行的传参 */}
          <Link
            to={{
              pathname: "/home",
              search: "?x=10",
              hash: "abc",
              state: {
                name: "zhangsan",
              },
            }}
          >
            home
          </Link>
        </li>
        <li>
          <Link to="/about?a=3&b=4">about</Link>
        </li>
        <li>
          <Link to="/detail/456">detail</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="/detail" element={<Detail />}>
          <Route path=":id" element={<Detail />}></Route>
        </Route>
      </Routes>
    </>
  );
};

export default App;
